package cc.rengu.igas.opms.core.service.trans.gateway;

import cc.rengu.igas.channel.cpcn.facade.response.CpcnMchntOrderPayRefundResponse;
import cc.rengu.igas.channel.cpcn.facade.result.Result;
import cc.rengu.igas.opms.common.constant.OpmsParamConstant;
import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.service.base.RefundService;
import cc.rengu.igas.opms.facade.request.CpcnOrderRefundRequest;
import cc.rengu.igas.opms.facade.response.CpcnOrderRefundResponse;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.share.core.service.channel.cpcn.CpcnMchntOrderPayRefundService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.MchntOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.MchntOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.CheckResultEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.realize.TransCustomImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.GlobalTransUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.List;
import java.util.Optional;

/**
 * 中金企业网关支付订单退款-未使用
 * T00PM060
 * 收银台->支付通道->中金网关
 *
 * @author fuyp
 * @date 2020/7/21.
 */
public class CpcnOrderRefundService extends RefundService implements TransCustomImpl {
    @Override
    protected Object bizBeforeCust() throws Exception {
        CpcnOrderRefundRequest cpcnOrderRefundRequest = new CpcnOrderRefundRequest();
        ConvertUtil.convertOutput(cpcnOrderRefundRequest);
        return cpcnOrderRefundRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        //获取请求数据
        CpcnOrderRefundRequest cpcnOrderRefundRequest = (CpcnOrderRefundRequest) request;
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();


        String instId = cpcnOrderRefundRequest.getHeader().getInstId();
        String userOrderId = cpcnOrderRefundRequest.getUserOrderId();
        /* 根据订单号查询订单流水号 */
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByUserOrderId(instId, userOrderId);
        if (userOrderInfo == null) {
            rglog.info("用户支付订单不存在");
            throw new BizException(RespCodeEnum.ORDER_NOEXIT.getRespCode(), RespCodeEnum.ORDER_NOEXIT.getRespDesc());
        }

        MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
        List<MchntOrderInfo> mchntOrderInfoList = mchntOrderInfoMapper.selectMchntOrderInfoBySysSeqNum(instId, userOrderInfo.getSysSeqNum());

        xmlTreeUtil.setXmlTreeStringValue("serialNumber", cpcnOrderRefundRequest.getMchntRefundId());//退款交易流水号
        xmlTreeUtil.setXmlTreeStringValue("paymentNo", cpcnOrderRefundRequest.getUserOrderId());//原支付交易流水号
        xmlTreeUtil.setXmlTreeStringValue("amount", cpcnOrderRefundRequest.getOrderAmt());//退款金额

        CpcnMchntOrderPayRefundService service = new CpcnMchntOrderPayRefundService();
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setMchntOrderInfoList(mchntOrderInfoList);
        orderInfo.setUserOrderInfo(userOrderInfo);
        service.callService(txnInfo, orderInfo, ShareParamConstant.CALL_CFG_DEFAULT_BIZ_SCENE, request);

        //组装响应信息
        CpcnOrderRefundResponse cpcnOrderRefundResponse = new CpcnOrderRefundResponse();

        BizResponse<CpcnOrderRefundResponse> bizResponse = new BizResponse<>();
        bizResponse.setResult(cpcnOrderRefundResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        CpcnOrderRefundResponse cpcnOrderRefundResponse = (CpcnOrderRefundResponse) bizResponse.getResult();
        ConvertUtil.convertInput(cpcnOrderRefundResponse);
    }

    @Override
    public ChannelCallCfg channelCallSceneDiscern(Object request, List<ChannelCallCfg> channelCallCfgList) throws Exception {
        Optional<ChannelCallCfg> channelCallCfgOpt = channelCallCfgList.stream().filter(item ->
                item.getBizScene().equals(ShareParamConstant.CALL_CFG_DEFAULT_BIZ_SCENE)).findFirst();
        return channelCallCfgOpt.orElse(null);
    }

    @Override
    public CheckResultEnum channelCallPreCheck(Object request, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        return CheckResultEnum.PASSED;
    }

    @Override
    public TransStatusEnum channelCallResultProc(Object request, ChannelCallCfg channelCallCfg, TransStatusEnum transStatusEnum, Object channelResponse, OrderInfo needUpdateOrderInfo) throws Exception {
        if (TransStatusEnum.TRANS_SUCC.equals(transStatusEnum) && null != channelResponse) {
            Result<CpcnMchntOrderPayRefundResponse> callResult = (Result<CpcnMchntOrderPayRefundResponse>) channelResponse;
            CpcnMchntOrderPayRefundResponse cpcnMchntOrderPayerAcctInfoQuery = callResult.getResult();
            //校验调用esb状态
            if (null == cpcnMchntOrderPayerAcctInfoQuery) {
                rglog.error("调用中金企业网关支付订单退款服务异常！");
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            if (OpmsParamConstant.DUBBO_OVERTIME.equals(cpcnMchntOrderPayerAcctInfoQuery.getRespCode())) {
                rglog.info("调用中金企业网关支付订单退款DUBBO接口-超时，respCode:<{}>,respDesc:<{}>", cpcnMchntOrderPayerAcctInfoQuery.getRespCode(), cpcnMchntOrderPayerAcctInfoQuery.getRespDesc());
                throw new BizException(RespCodeEnum.BIZ_TIMEOUT_ERROR.getRespCode(), RespCodeEnum.BIZ_TIMEOUT_ERROR.getRespDesc());
            } else if (!OpmsParamConstant.DUBBO_SUCCESS.equals(cpcnMchntOrderPayerAcctInfoQuery.getRespCode())) {
                rglog.info("调用中金企业网关支付订单退款DUBBO接口-失败，respCode:<{}>,respDesc:<{}>", cpcnMchntOrderPayerAcctInfoQuery.getRespCode(), cpcnMchntOrderPayerAcctInfoQuery.getRespDesc());
                throw new BizException(cpcnMchntOrderPayerAcctInfoQuery.getRespCode(), cpcnMchntOrderPayerAcctInfoQuery.getRespDesc());
            } else {
                rglog.info("调用中金企业网关支付订单退款DUBBO接口-成功，respCode:<{}>,respDesc:<{}>", cpcnMchntOrderPayerAcctInfoQuery.getRespCode(), cpcnMchntOrderPayerAcctInfoQuery.getRespDesc());
            }
        }
        return transStatusEnum;
    }
}
